using System;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class UIStyledItem : MonoBehaviour
{
	[Serializable]
	public class ItemStyleSetting
	{
		public UIStyledItem item;

		public string styleName;

		public GameObject gameObject;

		public bool hide;
	}

	[Serializable]
	public class StyleSet
	{
		public List<ItemStyleSetting> setting = new List<ItemStyleSetting>();

		public void Apply()
		{
			for (int i = 0; i < setting.Count; i++)
			{
				ItemStyleSetting itemStyleSetting = setting[i];
				if (itemStyleSetting.item != null)
				{
					itemStyleSetting.item.SetStyle(itemStyleSetting.styleName);
				}
				UITools.SetActive(itemStyleSetting.gameObject, !itemStyleSetting.hide);
			}
		}
	}

	public UIStyle.UIStyleId styleId = new UIStyle.UIStyleId();

	private void OnEnable()
	{
		UIStyle.onStyleChanged -= ApplyStyle;
		UIStyle.onStyleChanged += ApplyStyle;
		ApplyStyle();
	}

	private void OnDisable()
	{
		UIStyle.onStyleChanged -= ApplyStyle;
	}

	public void SetStyle(string styleName)
	{
		if (!(styleId.styleName == styleName))
		{
			styleId.styleName = styleName;
			ApplyStyle();
		}
	}

	public void ApplyStyle()
	{
		UIStyle.StyleClass style = styleId.style;
		UIWidget component = GetComponent<UIWidget>();
		if (style != null && !(component == null))
		{
			style.ApplyTo(component);
		}
	}
}
